<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateOrderTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('think_order');
        $table
            ->addColumn('order_number', 'string', [
                'limit' => 32,
                'default' => '',
                'comment' => '订单号',
            ])
            ->addIndex(['order_number'], ['unique' => true]) // 唯一索引
            ->addColumn('user_id', 'integer', ['comment' => '创建订单用户ID'])
            ->addColumn('goods_json', 'json', ['comment' => '订单购买商品信息JSON']) // JSON格式
            ->addColumn('amount', 'integer', ['default' => 0, 'comment' => '订单金额(分)'])
            ->addColumn('pay_amount', 'integer', ['null' => true, 'comment' => '订单实际付款金额(分)'])
            ->addColumn('status', 'integer', ['default' => 0, 'comment' => '订单状态(0预订单,1待付款,2已付款,3已发货,4已收货,5已完成,6已取消)'])
            ->addColumn('address_id', 'integer', ['null' => true, 'comment' => '收货地址ID'])
            ->addColumn('address_detail', 'text', ['null' => true, 'comment' => '收货地址详情'])
            ->addColumn('deliver_type', 'integer', ['default' => 0, 'comment' => '送达方式(0自提,1配送)'])
            ->addColumn('remarks', 'text', ['limit' => 255, 'default' => '', 'comment' => '订单备注'])
            ->addColumn('receive_time', 'datetime', ['null' => true, 'comment' => '要求的送达时间'])
            ->addColumn('created_at', 'datetime', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '创建日期'])
            ->addColumn('updated_at', 'datetime', ['null' => true, 'comment' => '修改日期'])
            ->addColumn('pay_time', 'datetime', ['null' => true, 'comment' => '支付日期'])
            ->addColumn('prepay_id', 'string', ['null' => true, 'comment' => '预支付订单ID'])
            ->addColumn('prepay_id_time', 'datetime', ['null' => true, 'comment' => '预支付订单ID生成时间'])
            ->create();
    }
}
